package demo.designPattern.aop.aspectJ.weavers;

import demo.spring.service.Account;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
 * 编译期植入：最简单的织入方式是编译时织入。
 * 当我们同时拥有切面的源代码和使用切面的代码时，AspectJ 编译器将从源代码编译，并生成一个织入后的类文件作为输出。
 * 随后，在执行代码时，织入过程生成的类会作为普通 Java 类加载到 JVM 中。
 *
 * <blockquote><pre>
 *  <plugin>
 *     <groupId>org.codehaus.mojo</groupId>
 *     <artifactId>aspectj-maven-plugin</artifactId>
 *     <version>1.14.0</version>
 *     <configuration>
 *         <complianceLevel>1.8</complianceLevel>
 *         <source>1.8</source>
 *         <target>1.8</target>
 *         <showWeaveInfo>true</showWeaveInfo>
 *         <verbose>true</verbose>
 *         <Xlint>ignore</Xlint>
 *         <encoding>UTF-8 </encoding>
 *     </configuration>
 *     <executions>
 *         <execution>
 *             <goals>
 *                 <!-- use this goal to weave all your main classes -->
 *                 <goal>compile</goal>
 *                 <!-- use this goal to weave all your test classes -->
 *                 <goal>test-compile</goal>
 *             </goals>
 *         </execution>
 *     </executions>
 * </plugin>
 * </pre></blockquote>
 */
public class CompileTimeDemo {

    private static final Logger logger = LoggerFactory.getLogger(CompileTimeDemo.class);

    private Account account;

    @Before
    public void before() {
        account = new Account();
    }

    @Test
    public void givenBalance20AndMinBalance10_whenWithdraw5_thenSuccess() {
        assertTrue(account.withdraw(5));
    }

    @Test
    public void givenBalance20AndMinBalance10_whenWithdraw100_thenFail() {
        assertFalse(account.withdraw(100));
    }
}
